package com.fyg.utils;

import com.fyg.domain.Menu;
import com.fyg.vo.MenuTreeVo;
import com.fyg.vo.MenuVo;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SystemConverter {
    private SystemConverter() {}
    public static List<MenuTreeVo> buildMenuSelectTree(List<Menu> menus){
        //全部
        List<MenuTreeVo> MenuTreeVos = menus.stream().map(m -> new MenuTreeVo(m.getId(), m.getMenuName(), m.getParentId(), null))
                .collect(Collectors.toList());

        List<MenuTreeVo> collect = MenuTreeVos.stream()
                //找出根目录
                .filter(o -> o.getParentId().equals(0L))
                //自己的id 以及全部
                .map(o -> o.setChildren(getChildList(MenuTreeVos, o)))
                .collect(Collectors.toList());
        return collect;
    }
    /**
     * 得到子节点列表
     */
    private static List<MenuTreeVo> getChildList(List<MenuTreeVo> list, MenuTreeVo option) {
        List<MenuTreeVo> options = list.stream().filter(o -> Objects.equals(o.getParentId(), option.getId()))
                .map(o -> o.setChildren(getChildList(list, o)))
                .collect(Collectors.toList());
        return options;
    }
}
